<?php
namespace app\customer\controller;
use think\Db;
class Channel extends Base{
    public function channel_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
	        $list=Db::name('channel')->where('cus_id',session('customer')['id'])->where('addtime','>',"{$fromtime}")->where('addtime','<',"{$totime}")->where('name|phone','like',"%{$key}%")->order('id desc')->paginate(10);
	        $lists=Db::name('channel')->where('cus_id',session('customer')['id'])->where('addtime','>',"{$fromtime}")->where('addtime','<',"{$totime}")->where('name|phone','like',"%{$key}%")->select();
        $items = $list->items();
        foreach ($items as $key => $value) {
            $items[$key]['res_nums']=Db::name('users')->where('invitephone',$items[$key]['phone'])->count();
            $items[$key]['dk_nums']=Db::name('orders')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->where('b.invitephone',$items[$key]['phone'])->count();
            $items[$key]['hk_nums']=Db::name('orders')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->where('a.state',21)->where('b.invitephone',$items[$key]['phone'])->count();
        }
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        $this->assign('items',$items);
        $this->assign('url',$_SERVER['HTTP_HOST']);
        return $this->fetch();
    }
    public function channel_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['name']=$param['name'];
            $data['cus_id']=session('customer')['id'];
            $data['phone']=$param['phone'];
            $data['password']=$param['password'];
            $data['state']=1;
            $data['addtime']=time();
            $res=Db::name('channel')->insert($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '调试']);
        }
    }
    public function channel_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $user=Db::name('channel')->where('id',$param['id'])->find();
            $this->assign('user',$user);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['name']=$param['name'];
            $data['phone']=$param['phone'];
            $data['password']=$param['password'];
            $data['state']=1;
            $data['uptime']=time();
            $res= Db::name('channel')->where(array('id'=>$param['id']))->update($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '更新成功']);
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('channel')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已禁止']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('channel')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已开启']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function channel_del(){
        $param = input('post.');
        $res= Db::name('channel')->where(array('id'=>$param['id']))->update(['status'=>3]);
        Db::name('product')->where(array('cus_id'=>$param['id']))->update(['status'=>3]);
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
    public function channel_dels(){
        $param = input('post.');
        $res= Db::name('channel')->where(array('id'=>$param['id']))->update(['status'=>3]);
        Db::name('product')->where(array('cus_id'=>$param['id']))->update(['status'=>3]);
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
}
